/**
 * zmn文章模块-文章编辑
 * 文件命名格式：zmn.模块.表/功能.jas
 * @author duanzuocai
 * @since 2018/10/22
 */
layui.define(['form', 'layedit'], function (exports) {
    var $ = layui.$;
    var form = layui.form;
    var layer = layui.layer;

    form.render(null, "zmn-form");

    // 自定义验证规则
    form.verify({
       isPositiveNumber: function(value,item){
           if (!/^[1-9]\d*$/.test(value)) {
                return "排序值只能是正整数";
            }
           if(Number(value)>100000){
               return "排序最大100000";
           }
        }
    });

    doRenderAnswerDiv(json.question.answerType);

    // 提交
    form.on('submit(zmn-form-submit)', function(obj){
        let answerArr = new Array();
        let categoryArr = new Array();
        let bizTypeArray = new Array();

        let node = $('input[name="answer"]');
        for(let k in node){
            if(node[k].checked)
                answerArr.push(node[k].value);
        }
        if (obj.field.answerType == 1 || obj.field.answerType == 2) {
            if(answerArr.length == 0){
                layer.msg("答案不能为空");
                return false;
            }
        }

        let type = $('input[name="category"]');
        for(let k in type){
            if(type[k].checked)
                categoryArr.push(type[k].value);
        }
        if(categoryArr.length == 0){
            layer.msg("回访类别不能为空");
            return false;
        }
        let bizType = $('input[name="bizType"]');
        for(let k in bizType){
            if(bizType[k].checked)
                bizTypeArray.push(bizType[k].value);
        }
        if(bizTypeArray.length == 0){
            layer.msg("业务类型不能为空");
            return false;
        }
        var openId = layer.open({type: 3});
        var data = obj.field;
        var url = "/conf/reply/question/addEdit.action";
        data.bizType =bizTypeArray.join(",");
        if (obj.field.answerType == 1 || obj.field.answerType == 2) {
            data.answerIdStr = '$'+answerArr.join("$")+'$';
        } else if (obj.field.answerType == 3){
            data.answerIdStr = '';
        }

        data.answerCategStr = '$'+categoryArr.join("$")+'$';

        $.ajax({
            async: false,
            type: "POST", url: url,
            contentType: "application/json", dataType: "json", data: JSON.stringify(data),
            success:function(result) {
                layer.close(openId);
                layer.msg(result.message);
                if(result.status == 200) {
                    layer.msg(result.message,{time: 1000},()=>{
                        zmnLayUtil.closeThisIframeMsg(parent, result.message, "zmn-table");
                        parent.location.reload();
                    });

                } else {
                    layer.msg(result.message, {"icon": 2});
                }
            },
            error:function(XMLHttpRequest, textStatus, errorThrown){
                layer.close(openId);
            }
        })
        return false;
    });

    form.on('radio(answerType)', function (data) {
        let value = data.value;
        doRenderAnswerDiv(value);
    });

    // 答案类型 单选/多选/手动输入 根据选择的答案类型渲染
    function doRenderAnswerDiv(value){
        // 按固定金额
        if (value == null || value == 1 || value == 2) {
            $("#multiChooseDiv").show();
        } else if (value == 3) {
            $("#multiChooseDiv").hide();
            let node = $('input[name="answer"]');
            for(var i = 0; i < node.length; i++){
                $(node[i]).prop("checked",false);
            }
        }
        form.render(null, "zmn-form");
    }

    //点击取消
    $(document).on('click', '#cancel', function () {
        let index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
        parent.layer.close(index);
    });

    //对外暴露的接口
    exports('oms_conf_reply_question_detail', {});
});
